package com.yanqu.road.server.cmd.activity.xiuxian;

import com.yanqu.road.entity.activity.tongbing.config.TongBingConfig;
import com.yanqu.road.entity.activity.xiuxian.config.XiuXianConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TongBingProto;
import com.yanqu.road.pb.activity.XiuXianProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.server.manger.activity.xiuxian.XiuXianMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_XIU_XIAN_GET_RANK_REWARD, desc = "领取榜单奖励")
public class XiuXianGetRankRewardCmd extends AbstractCommand {

    private XiuXianProto.XiuXianGetRankRewardReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = XiuXianProto.XiuXianGetRankRewardReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        XiuXianConfig config = XiuXianMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(config.getActivityInfo())) {
            ret = GameErrorCode.E_XIU_XIAN_NO_OPERATE_TIME;
            return;
        }

        XiuXianProto.CrossXiuXianGetRankRewardReqMsg.Builder builder = XiuXianProto.CrossXiuXianGetRankRewardReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setReq(reqMsg.toBuilder());
        player.sendPacket(CrossProtocol.C_XIU_XIAN_GET_RANK_REWARD, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        XiuXianProto.XiuXianGetRankRewardRespMsg.Builder respMsg = XiuXianProto.XiuXianGetRankRewardRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_XIU_XIAN_GET_RANK_REWARD, respMsg);
    }
}
